<?php

namespace app\manage\controller;

use think\Db;
use app\manage\model\Manage as ManageModel;
use think\Loader;

class Manage extends Common
{
    public function index($tab = '1', $id = 0)
    {
        $manageModel = new ManageModel;
        if (request()->isPost()) {
            $data = input('post.');
            //新增管理员后台验证
            $validate = Loader::validate('Manage');

            if (!$validate->check($data)) {
                return error($validate->getError());
                die;
            }
            $data['manage_pwd'] = sha1(sha1($data['manage_pwd']));
            $data['manage_lip'] = request()->ip();
            $data['manage_ltime'] = TIME();
            $data['manage_status'] = 1;
            if ($manageModel->allowField(true)->save($data)) {
                return success('新增成功!', url('index', ['tab' => 1]));
            } else {
                return error('新增失败!');
            }
        } else {
            $managelist = Db::name('manage')->order('id asc')->select();

            $this->assign('managelist', $managelist);

            if ($tab == 3) {
                $manage_info = $manageModel->where('id', $id)->find();
                if ($manage_info != null) {
                    $this->assign('manage_info', $manage_info);
                }

            }
        }
        return view();
    }

    //修改管理员信息
    public function edit()
    {

        if (request()->isPost()) {
            $manageModel = new ManageModel;
            $data = input('post.');

            //编辑管理员后台验证
            $validate = Loader::validate('Manage');

            if (!$validate->scene('edit')->check($data)) {
                return error($validate->getError());
                die;
            }
            $map['id'] = $data['id'];
            $map['manage_name'] = $data['manage_name'];
            if (!empty($data['manage_pwd'])) {
                $map['manage_pwd'] = sha1(sha1($data['manage_pwd']));
            }
            $map['manage_mobile'] = $data['manage_mobile'];
            $map['manage_status']=$data['manage_status'];
            $affected = $manageModel->isUpdate(true)->allowField(true)->save($map);
            if ($affected !== false) {
                return success('修改成功!', url('index', ['tab' => 1]));
            } else {
                return error('修改失败!');
            }
        } else {
            return error('非法提交');
        }
    }

    public function del()
    {
        $del = db('manage')->delete(input('id'));
        if ($del) {
            return success('删除成功!', url('index', ['tab' => 1]));
        } else {
            return error('删除失败!');
        }
    }

    public function status(){
        if (request()->isPost()) {
            $data = input('post.');
            $manageModel = new ManageModel;
            if ($manageInfo = $manageModel->where('id', $data['id'])->find()) {

                if ($manageInfo['manage_status'] == 1) {
                    $manage = Db::name('manage')->where('id', $data['id'])->update(['manage_status'=>2]);
                }
                if($manageInfo['manage_status'] == 2){
                    $manage = Db::name('manage')->where('id', $data['id'])->update(['manage_status'=>1]);
                }
                if ($manage) {
                    return 1;
                } else {
                    return 0;
                }
            }else{
                return -1;
            }
        }
    }
}
